﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.Data;
using MLM.AdminCP.Class;

namespace MLM.AdminCP.Pages
{
    public partial class UserHierarchy : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            ViewHierarchyBtn.Click += new EventHandler(ViewHierarchyBtn_Click);
        }

        void ViewHierarchyBtn_Click(object sender, EventArgs e)
        {
            try
            {
                long userId = long.Parse(UserIDTxt.Text); 
                MLMDataContext ct = new MLMDataContext();
                var user = ct.Users.FirstOrDefault(it => it.UserId == userId);
               
                Template = "[{v:'" + userId + "',f:'" + user.FirstName + " " + user.LastName + "<p>" + userId + "</p>'},'',''],";
                GetHierarchy(userId);
            }
            catch (Exception)
            {
                Message.ShowError("Error! Please check yout input!");
            }
        }

        public static string Template = "";
        public static void GetHierarchy(long sponsorid)
        {
            MLMDataContext ct = new MLMDataContext();
            IQueryable<User> queryable = ct.Users.Where(it => it.SponsorID == sponsorid);
            foreach (var user in queryable)
            {
                Template += "[{v:'" + user.UserId + "',f:'" + user.FirstName + " " + user.LastName + "<p>" + user.UserId + "</p>'},'" + sponsorid + "',''],";
                GetHierarchy(user.UserId);
            }
        }
    }
}